/*
 * @lc app=leetcode.cn id=661 lang=cpp
 *
 * [661] 图片平滑器
 */
#include "include.h"
// @lc code=start
class Solution {
public:
    vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
        m = img.size();
        n = img[0].size();
        vector<vector<int>> res(m, vector<int>(n, 0));
        for (int row=0;row<m;row++){
            for (int col=0;col<n;col++){
                convolutionKernel(img, res, row, col);
            }
        }
        return res;
    }
private:
    int m, n;
    void convolutionKernel(
        const vector<vector<int>>& img,
        vector<vector<int>>& res,
        int row, int col
        ){
        int sum = 0;
        int counter = 0;
        for (int i=0;i<9;i++){
            int currentRow = row + direction_[i][0];
            int currentCol = col + direction_[i][1];
            if (insideImg(currentRow, currentCol)){
                counter++;
                sum += img[currentRow][currentCol];
            }
        }
        res[row][col] = sum/counter;
    }
    inline bool insideImg(int row, int col){
        return row>=0 and row<m and col>=0 and col<n;
    }
    const int direction_[9][2] = {
        {-1, -1},
        {-1,  0},
        {-1,  1},
        { 0, -1},
        { 0,  0},
        { 0,  1},
        { 1, -1},
        { 1,  0},
        { 1,  1}
    };
};
// @lc code=end

